package com.ruoyi.framework.reptile.webmagic.jingcai.rank.cba;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.enums.JcTeamType;
import com.ruoyi.common.enums.Yn;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.reptile.webmagic.jingcai.AbstractJingCaiPipeline;
import com.ruoyi.system.domain.JcTeam;
import com.ruoyi.system.handler.reptile.SinaReptileHandler;
import com.ruoyi.system.service.IJcTeamService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.naming.factory.SendMailFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dzl
 * @Description 新浪抓取cba球队处理器
 * @date 2024年07月18日 18:37:00
 */
public class SinaCbaRankPipeline extends AbstractJingCaiPipeline implements Pipeline {

    private static final Logger log = LoggerFactory.getLogger(SinaCbaRankPipeline.class);

    @Override
    public void process(ResultItems resultItems, Task task) {
        String jcData = resultItems.get("jcData").toString();
        List<CbaRankResult> list = JSON.parseArray(jcData, CbaRankResult.class);
        log.info("cba球队排行榜爬取数据list:{}", JSON.toJSONString(list));
        IJcTeamService jcTeamService = getJcTeamService();
        if (CollectionUtils.isNotEmpty(list)) {
            // 获取赛季
            String season = list.get(0).getSeason();
            if (StringUtils.isEmpty(season)) {
                log.info("season为空不处理，直接返回");
                return;
            }
            // 处理球队排名
            JcTeam queryParam = new JcTeam();
            queryParam.setSeason(season);
            queryParam.setType(JcTeamType.TYPE_CBA.getType());
            handleJcTeam(queryParam, buildUpdateList(list));
        }
    }

    /**
     * 构建更新的报文
     *
     * @param list
     * @return
     */
    private List<JcTeam> buildUpdateList(List<CbaRankResult> list) {
        List<JcTeam> jcTeamList = list.stream().map( cbaRankResult -> {
            JcTeam jcTeam = new JcTeam();
            jcTeam.setTeam(cbaRankResult.getTeam_name());
            jcTeam.setType(JcTeamType.TYPE_CBA.getType());
            jcTeam.setRanking(cbaRankResult.getRank());
            jcTeam.setSeason(cbaRankResult.getSeason());
            jcTeam.setHomeWins(cbaRankResult.getHome_wins());
            jcTeam.setHomeLose(cbaRankResult.getHome_losses());
            jcTeam.setOpponentWins(cbaRankResult.getRoad_wins());
            jcTeam.setOpponentLose(cbaRankResult.getRoad_losses());
            jcTeam.setAvgScores(cbaRankResult.getPoints_for());
            jcTeam.setAvgLoseScores(cbaRankResult.getPoints_against());
            jcTeam.setYn(Yn.ENABLE.getCode());
            jcTeam.setCreator("webmagic");
            jcTeam.setModifier("webmagic");
            return jcTeam;
        }).collect(Collectors.toList());
        return jcTeamList;
    }
}
